package edu.pw.treegrid.client.report.model;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.data.fields.DataSourceEnumField;
import com.smartgwt.client.data.fields.DataSourceImageField;
import com.smartgwt.client.data.fields.DataSourceTextField;

import edu.pw.treegrid.client.report.events.FetchReportColumnCallback;
import edu.pw.treegrid.shared.Configuration;
import edu.pw.treegrid.shared.ReportColumnCategory;
import edu.pw.treegrid.shared.ReportColumnDescription;
import edu.pw.treegrid.shared.ReportColumnType;

public class ReportMetaModelDataSource extends RestDataSource {
	private String reportId;
	
	public ReportMetaModelDataSource(String reportId) {
		this.reportId = reportId;
		setID("reportMetaModel-" + reportId);
		DataSourceTextField name = new DataSourceTextField(ReportColumnDescription.NAME);
		name.setPrimaryKey(true);
		DataSourceTextField title = new DataSourceTextField(ReportColumnDescription.TITLE);
		DataSourceEnumField type = new DataSourceEnumField(ReportColumnDescription.TYPE);
		DataSourceImageField imageSrc = new DataSourceImageField(ReportColumnDescription.IMAGE_SRC);
		DataSourceEnumField category = new DataSourceEnumField(ReportColumnDescription.CATEGORY);
		DataSourceTextField description = new DataSourceTextField(ReportColumnDescription.DESCRIPTION);
		DataSourceTextField width = new DataSourceTextField(ReportColumnDescription.WIDTH);
		setFields(name, title, type, imageSrc, category, description, width);
		setFetchDataURL(Configuration.PREFIX + Configuration.META_MODEL_URL);
	}
	
	@Override
    protected Object transformRequest(DSRequest dsRequest) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(Configuration.REPORT_ID_PARAM, reportId);
		dsRequest.setParams(params);
        return super.transformRequest(dsRequest);
    }
	/*
    @Override
    protected void transformResponse(DSResponse response, DSRequest request, Object data) {
        super.transformResponse(response, request, data);
    }*/
    
    public void fetchReportColumns(final FetchReportColumnCallback callback) {
		
		this.fetchData(null, new DSCallback() {			
			public void execute(DSResponse response, Object rawData, DSRequest request) {
				List<ReportColumnDescriptionClient> columns = new LinkedList<ReportColumnDescriptionClient>();
				Record[] data = response.getData();
				for(int i=0;i<data.length;++i) {
					ReportColumnDescriptionClient rcd = new ReportColumnDescriptionClient();
					rcd.setName(data[i].getAttribute(ReportColumnDescription.NAME));
					rcd.setTitle(data[i].getAttribute(ReportColumnDescription.TITLE));
					rcd.setImageSrc(data[i].getAttribute(ReportColumnDescription.IMAGE_SRC));
					rcd.setType(ReportColumnType.valueOf(data[i].getAttribute(ReportColumnDescription.TYPE)));
					rcd.setCategory(ReportColumnCategory.valueOf(data[i].getAttribute(ReportColumnDescription.CATEGORY)));
					rcd.setFormat(data[i].getAttribute(ReportColumnDescription.FORMAT));
					rcd.setDescription(data[i].getAttribute(ReportColumnDescription.DESCRIPTION));
					rcd.setWidth(data[i].getAttribute(ReportColumnDescription.WIDTH));
					columns.add(rcd);		
				}
				callback.reportColumns(columns);
			}
		});
	}
}
